package com.eeepay.eeepay_v2.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2.adapter.PaperAdapter;
import com.eeepay.eeepay_v2.model.PaperFile;
import com.eeepay.eeepay_v2.model.PapersInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.SPUtils;
import com.eeepay.eeepay_v2.util.StatuUtils;
import com.eeepay.eeepay_v2.view.ScrollGridView;
import com.eeepay.v2_library.ui.ABBaseActivity;
import com.eeepay.v2_library.util.ScreenSwitch;
import com.eeepay.v2_library.view.TitleBar;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import cn.eeepay.api.grpc.nano.MerAddModular;

/**
 * Created by zw on 2016/3/7 0007.
 * 证件资料界面
 */
public class PapersActivity extends ABBaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener, TitleBar.LeftBtnOnClickListener {

    private ScrollGridView paperGv;
    private Button nextBtn;
    private TitleBar titleBar;

    //    private List<PapersInfo> paperList = new ArrayList<>();
    private List<PaperFile> datas = new ArrayList<>();
    ;
    //    private String[] filepaths;
//    private MerAddModular.MerFile[] merFiles;
    private PaperAdapter adapter;
    private PaperFile file;
    //    private PapersInfo info;
    private String filePath;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_complete_papers;
    }

    @Override
    protected void initWidget() {
        paperGv = getViewById(R.id.gv_papers);
        nextBtn = getViewById(R.id.btn_papers_next);
        titleBar = getViewById(R.id.tb_paper);
    }

    @Override
    protected void eventOnClick() {
        titleBar.setLeftOnClickListener(this);
        adapter = new PaperAdapter(this);
        datas = SPUtils.getList(Constant.KEY.PAPER_FILE);
//        if (datas != null && datas.size() > 0) {
//            filepaths = new String[datas.size()];
//            for (int i = 0; i < datas.size(); i++) {
//                file = datas.get(i);
//                info = new PapersInfo();
//                info.setTipName(file.getTipName());
//                info.setFileName(file.getFileName());
//                info.setItemId(file.getItemId());
//                info.setPhoto(file.getPhoto());
//                info.setRemark(file.getRemark());
//                Log.d("itemId"," id --> " +file.getTipName() +" | "+ file.getFileName());
//                if (file.getFilePath() != null) {
//                    info.setFilePath(file.getFilePath());
//                }
//                filepaths[i] = file.getFilePath();
//                paperList.add(info);
//            }
//        }
        Collections.sort(datas, new Comparator<PaperFile>() {//按照进件项排序
            @Override
            public int compare(PaperFile lhs, PaperFile rhs) {
                Integer item1 = Integer.valueOf(lhs.getItemId());
                Integer item2 = Integer.valueOf(rhs.getItemId());
                return item1.compareTo(item2);
            }
        });
        adapter.addAll(datas);
        paperGv.setAdapter(adapter);
        paperGv.setOnItemClickListener(this);
        nextBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_papers_next:
                saveDatas();
                break;
        }
    }

    /**
     * 保存当前的数据
     */
    public void saveDatas() {
        long startTime = System.currentTimeMillis();
        Intent intent = new Intent();
//        boolean flag = false;
//        merFiles = new MerAddModular.MerFile[datas.size()];
        String filepath;
        String tipName;
        for (int i = 0; i < datas.size(); i++) {
            filepath = datas.get(i).getFilePath();
            tipName = datas.get(i).getTipName();
            if (filepath == null){
                showToast("请上传" + tipName + " 的证件资料");
                setResult(RESULT_OK, intent);
                return;
            }
        }
//            merFile = new MerAddModular.MerFile();
//            tipName = adapter.getItem(i).getTipName();
//            filepath = adapter.getItem(i).getFilePath();
//            if (filepath == null) {
//                showToast("请上传" + tipName + " 的证件资料");
//                flag = false;
//                setResult(RESULT_OK, intent);
//                return;
//            } else {
//                merFile.fileName = adapter.getItem(i).getFileName();
//                merFiles[i] = merFile;
//                flag = true;
//            }
        Log.d("paper", "图片耗时1:" + (System.currentTimeMillis() - startTime) / 1000 + "秒");
//        if (flag) {
//            for (int i = 0; i < datas.size(); i++) {
//                file = datas.get(i);
//                if (file != null) {
//                    Log.d("FileName", "FileName>>> " + merFiles[i].fileName);
//                    file.setFileName(String.valueOf(merFiles[i].fileName));
//                    file.setFilePath(filepaths[i]);
//                }
//            }
            SPUtils.saveList(datas, Constant.KEY.PAPER_FILE);
            StatuUtils.getInstance().setPaper("已完成");
            ScreenSwitch.switchActivity(this, BasicDataActivity.class, null, 0);
            finish();
//        }
        long endTime = System.currentTimeMillis();
        Log.d("paper", "图片耗时2:" + (endTime - startTime) / 1000 + "秒");
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        if (data != null) {
            filePath = data.getStringExtra(Constant.FILE_PATH);
            if (filePath != null) {
//                file = datas.get(requestCode);
//                file.setFilePath(filePath);
//                if (datas.size() > requestCode && datas.get(requestCode) != null) {
                file = datas.get(requestCode);
                file.setFilePath(filePath);
//                }
//                else {
//                    file = new PaperFile();
//                    file.setFilePath(filePath);
//                    datas.add(file);
//                }
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        file = datas.get(position);
//        if ("1".equals(file.getPhoto())) {
//            selectCamera();
//        } else {
//
//        }
        Bundle bundle = new Bundle();
        bundle.putInt("num", position);
        bundle.putString("photo", file.getPhoto());//拍照或者拍照&相册
        bundle.putString("tipName", file.getTipName());//标题
        bundle.putString("remark", file.getRemark());//说明
        bundle.putString("filePath", file.getFilePath());
        bundle.putString(Constant.ITEM_ID, file.getItemId());//标题
        goActivityForResult(ChooseImageActivity.class, bundle, position);
    }

//    @Override
//    protected String getPhotoPath() {
//        return "eeepay";
//    }
//
//    @Override
//    protected void selectPhotoPathResult(File imgFile, Bitmap bitmap) {
//
//    }


    @Override
    public void onLeftClick(View view) {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
